package main.java.controller;

import main.java.pojo.User;
import main.java.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user){
        boolean flag = userService.login(user);
        if(flag){
            return "index";
        }else{
            return "login";
        }
    }

    @RequestMapping("/list")
    public String findAll(Model model){
        List<User> users = userService.findAll(null, 1, 10);
        model.addAttribute("users", users);
        return "user/list";
    }

    @RequestMapping("/toadd")
    public String toAdd(){
        return "user/add";
    }

    @RequestMapping("/addSave")
    public String addUser(User user){
        int result = userService.addUser(user);
        if (result > 0){
            return "redirect:/user/list";
        }else {
            return "error";
        }
    }

    @RequestMapping("/delete/{id}")
    public String deleteUser(int id){
        userService.deleteUser(id);
        return "index";
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdateUser(int id){
        return "index";
    }

    @RequestMapping("/updateSave")
    public String updateUser(){
        return "index";
    }
}
   